package com.yunche.yunchebusiness.ui.activity.brand

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.alibaba.fastjson.JSONObject
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yunche.yunchebusiness.R
import com.yunche.yunchebusiness.base.BaseActivity
import com.yunche.yunchebusiness.common.Constant
import com.yunche.yunchebusiness.entity.BrandInfoEntity
import com.yunche.yunchebusiness.entity.ModelEntity
import com.yunche.yunchebusiness.entity.SeriesEntity
import com.yunche.yunchebusiness.mvp.contract.SeriesContract
import com.yunche.yunchebusiness.mvp.presenter.SeriesPresenter
import com.yunche.yunchebusiness.network.RequestJson
import kotlinx.android.synthetic.main.activity_series.*


class SeriesActivity : BaseActivity(), SeriesContract.View, BaseQuickAdapter.OnItemClickListener {
    private val mPresenter: SeriesPresenter by lazy {
        SeriesPresenter()
    }
    var carInfoEntity: BrandInfoEntity? = null
    var mAdapter: SeriesAdapter? = null

    override fun setLayout(): Int = R.layout.activity_series

    override fun initView() {
        mPresenter.attachView(this)
        carInfoEntity = intent.getSerializableExtra(Constant.BRAND_INFO_ENTITY) as BrandInfoEntity
        rv_series.layoutManager = LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
    }

    override fun initData() {
        var parmar = JSONObject()
        parmar["brand"] = carInfoEntity?.brandCode
        mPresenter.getSeries(RequestJson.getRequestBody(parmar.toJSONString()))
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

        var entity = adapter!!.data[position] as SeriesEntity
        var intent = Intent(mContext!!,ModelActivity::class.java)
        carInfoEntity?.seriesIcon = entity.imgUrl
        carInfoEntity?.seriesName = entity.name
        carInfoEntity?.seriesCode = entity.code
        carInfoEntity?.seriesId = "${entity.id}"
        intent.putExtra(Constant.BRAND_INFO_ENTITY, carInfoEntity)
        startActivityForResult(intent, Constant.SELECT_MODEL_RESULT_CODE)

//        var seriesEntitys = adapter?.data as List<SeriesEntity>
//        var parmar = JSONObject()
//        parmar["series"] = seriesEntitys[position].code
//        mPresenter.getModel(RequestJson.getRequestBody(parmar.toJSONString()))
    }


    override fun initEvent() {
    }

    override fun getSeriesSuccess(seriesEntity: List<SeriesEntity>) {
        if (mAdapter == null) {
            mAdapter = SeriesAdapter(R.layout.item_series, seriesEntity)
            (mAdapter as SeriesAdapter).onItemClickListener = this
            (mAdapter as SeriesAdapter).isFirstOnly(false)
            (mAdapter as SeriesAdapter).openLoadAnimation(BaseQuickAdapter.SCALEIN)
            rv_series.adapter = mAdapter
        }
    }

    override fun showError(msg: String) {
    }

    override fun getModelSuccess(modelEntity: List<ModelEntity>) {
        var modelTitle = arrayListOf<String>()
        for (modelEntity in modelEntity) {
            modelTitle.add(modelEntity.name)
        }

        val pvOptions = OptionsPickerBuilder(mContext,
            OnOptionsSelectListener { options1, _, _, _ ->
                //返回的分别是三个级别的选中位置
            }).build<String>()
        pvOptions.setPicker(modelTitle)
        pvOptions.show()

    }

    override fun showLoading() {
        srl_series.isRefreshing = true
    }

    override fun hideLoading() {
        srl_series.isRefreshing = false

    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter.detachView()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (resultCode) {
            Constant.SELECT_MODEL_RESULT_CODE -> {
                var intent = Intent()
                intent.putExtras(data)
                setResult(Constant.SELECT_MODEL_RESULT_CODE, intent)
                finish()
            }
        }
    }

    inner class SeriesAdapter constructor(id: Int, data: List<SeriesEntity>) :
        BaseQuickAdapter<SeriesEntity, BaseViewHolder>(id, data) {

        override fun convert(helper: BaseViewHolder?, item: SeriesEntity?) {

            helper?.setText(R.id.tv_series_name, item!!.name)

            Glide.with(mContext).load(item?.imgUrl).into(helper!!.getView(R.id.iv_series_cover))

        }

    }


}
